<?php
/**
 * Created by Daniel X -- PhpStorm.  2020/7/24 15:30
 */

namespace App\Services;

use App\Http\Controllers\Traits\TransFormer;
use App\Models\InjuryOne;
use App\Models\InjuryTwo;
use App\Models\InjuryTwoScene;
use App\Models\InjuryTwoTrait;
use App\Models\Iss;
use App\Models\Media;
use App\Models\Patient;
use Illuminate\Support\Facades\DB;

class InjuryService
{
    use TransFormer;
    protected $one;
    protected $twoTrait;
    protected $twoScene;
    protected $two;
    protected $patient;
    protected $iss;
    protected $media;
    public function __construct(InjuryOne $one,InjuryTwoTrait $twoTrait,InjuryTwo $two,InjuryTwoScene $twoScene,Patient $patient,Iss $iss,Media $media)
    {
        $this->one = $one;
        $this->twoTrait = $twoTrait;
        $this->twoScene = $twoScene;
        $this->two = $two;
        $this->patient = $patient;
        $this->iss = $iss;
        $this->media = $media;
    }

    public function saveOne($data)
    {
        self::checkPatientOK($data['patient_id']);

        $res = $this->one->where(['patient_id'=>$data['patient_id']])->first();
        if($res){
            // 更新
            $this->one->where(['patient_id'=>$data['patient_id']])->update($data);
            $oneId = $res->one_id;
        }else{
            // 插入
            $data['create_time'] = date("Y-m-d H:i:s");
            $oneId = $this->one->insertGetId($data);
            if(!$oneId){
                $this->transFormerE(1002002, '初次检伤添加失败');
            }
        }
        return ['one_id'=>$oneId];
    }

    public function saveTwoTrait($data)
    {
        $res = $this->twoTrait->where(['patient_id'=>$data['patient_id']])->first();
        if($res){
            // 更新
            $this->twoTrait->where(['patient_id'=>$data['patient_id']])->update($data);
            $trait_id = $res->trait_id;
        }else{
            // 插入
            $data['create_time'] = date("Y-m-d H:i:s");

            $trait_id = $this->twoTrait->insertGetId($data);
            if(!$trait_id){
                $this->transFormerE(1002004, '二次检伤-伤情特点-添加失败');
            }
            // 更新 二次检伤 信息 综合 表  1=第一步的状态，2=......... 到 4
            $type = 1;
            $two_id = self::createTwoInjury($type,$data['patient_id']);
            $this->twoTrait->where(['trait_id'=>$trait_id])->update(['two_id'=>$two_id]);
        }
        return ['trait_id'=>$trait_id];
    }

    public function saveTwoSceneB($data)
    {
        self::checkPatientOK($data['patient_id']);

        $two_id = self::createTwoInjury($data['type'],$data['patient_id']);

        $res = $this->twoScene->where(['patient_id'=>$data['patient_id'],'type'=>$data['type']])->first();
        if($res){
            // 更新
            $this->twoScene->where(['patient_id'=>$data['patient_id'],'type'=>$data['type']])->update($data);
            $scene_id = $res->scene_id;
        }else{
            // 插入
            $data['create_time'] = date("Y-m-d H:i:s");
            $data['two_id'] = $two_id;
            $scene_id = $this->twoScene->insertGetId($data);
            if(!$scene_id){
                $this->transFormerE(1002005, '二次检伤-现场验伤-添加失败');
            }
        }
        return ['scene_id'=>$scene_id];
    }

    /**
     * 二次检伤 信息综合表 更新
     * @param $type
     * @param $patient_id
     * @return mixed
     */
    public function createTwoInjury($type,$patient_id){
        // 创建之前需要 根据患者ID 来判断是否存在
        $status = $type==1?'one_status':($type==2?'two_status':($type==3?'three_status':'four_status'));
        $res = $this->two->where(['patient_id'=>$patient_id])->get();
        if($res->isEmpty()){
            $tData = [
                'patient_id' => $patient_id,
                'create_time' => date("Y-m-d H:i:s"),
                $status => 2
            ];
            $two_id = $this->two->insertGetId($tData);
        }else{
            $this->two->where(['patient_id'=>$patient_id])->update([$status=>2]);
            $two_id = $res[0]->two_id;
        }
        return $two_id;
    }

    /**
     * 过滤数组中的 null 或 指定值
     * @param $data
     * @return mixed
     */
    public function delNullData($data)
    {
        foreach ($data as $kk => $vv){
            if($vv == null){
                $data[$kk] = "";
            }
        }
        return $data;
    }

    /**
     * 检测患者ID有效性
     * @param $patient_id
     * @return mixed
     */
    public function checkPatientOK($patient_id)
    {
        $res = $this->patient->where(['patient_id'=>$patient_id])->first();
        if(!$res){
            $this->transFormerE(1002005, '请核对患者ID');
        }
        return $res;
    }

    public function createTwoInjuryTable($patient_id)
    {
        $twoData = [
            'patient_id' => $patient_id,
            'create_time' => date("Y-m-d H:i:s"),
        ];
        $this->two->insert($twoData);
    }

    public function getTwoInjuryInfo($data)
    {
        $this->checkPatientOK($data['patient_id']);
        $res = $this->two->where(['patient_id'=>$data['patient_id']])->get();
        $res = $res[0]->toArray();
        foreach ($res as $kk => $vv){
            if(empty($vv) || $vv == null){
                $res[$kk] = "";
            }
        }
        return $res;
    }

    public function getTwoInjuryTrait($data)
    {
        $this->checkPatientOK($data['patient_id']);
        $res = $this->twoTrait->where(['patient_id'=>$data['patient_id']])->get();
        $res = $res[0]->toArray();
        foreach ($res as $kk => $vv){
            if(empty($vv) || $vv == null){
                $res[$kk] = "";
            }
        }
        if(!empty($res['injury_site'])){
            $res['injury_site'] = explode(',',$res['injury_site']);
        }
        if(!empty($res['accident_type'])){
            $res['accident_type'] = explode(',',$res['accident_type']);
        }
        if(!empty($res['damage_type'])){
            $res['damage_type'] = explode(',',$res['damage_type']);
        }
        return $res;
    }

    public function getTwoInjuryScene($data)
    {
        $this->checkPatientOK($data['patient_id']);
        $res = $this->twoScene->where(['patient_id'=>$data['patient_id'],'type'=>$data['type']])->get();
        if($res->isEmpty()){
            $this->transFormerE(1002009, '查询数据为空');
        }
        $res = $res[0]->toArray();
        foreach ($res as $kk => $vv){
            if(empty($vv) || $vv == null){
                $res[$kk] = "";
            }
        }
        return $res;
    }

    public function getInjuryMess($data)
    {
        // 获取用户基本信息
        $userData = $this->checkPatientOK($data['patient_id']);
        $userArr['name'] = empty($userData['name'])?"":$userData['name'];
        $userArr['age'] = empty($userData['age'])?"":$userData['age'];
        $userArr['gender'] = $userData['gender'] == 1?'男':($userData['gender'] == 2?'女':'');

        $where = ['patient_id'=>$data['patient_id']];
        // 获取初次检伤状态
        $oneData = $this->one->where($where)->select('one_id','status')->get();
        if($oneData->isEmpty()){
            $oneArr['status'] = 1;
            $oneArr['one_id'] = "";
        }else{
            $oneArr['one_id'] = $oneData[0]->one_id;
            $oneArr['status'] = $oneData[0]->status;
        }

        $twoData = $this->two->where($where)->get();
        if($twoData->isEmpty()){
            $twoArr['status'] = 1;
            $twoArr['two_id'] = "";
            $twoArr['level'] = "";
        }else{
            $twoArr['two_id'] = $twoData[0]->two_id;
            $twoArr['level'] = $twoData[0]->level;
            $t_one_status = $twoData[0]->one_status;
            $t_two_status = $twoData[0]->two_status;
            $t_three_status = $twoData[0]->three_status;
            $t_four_status = $twoData[0]->four_status;
            if($t_one_status==2 && $t_two_status==2 && $t_three_status==2 && $t_four_status==2){
                $twoArr['status'] = 2;
            }else{
                $twoArr['status'] = 1;
            }
        }

        // 三次检伤
        $threeArr = $this->iss->where($where)->select('iss_count','ais_count','status')->get();
        if($threeArr->isEmpty()){
            $tArr['iss_count'] = 0;
            $tArr['ais_count'] = 0;
            $tArr['status'] = 0;
        }else{
            $tArr['iss_count'] = $threeArr[0]->iss_count;
            $tArr['ais_count'] = $threeArr[0]->ais_count;
            $tArr['status'] = $threeArr[0]->status;
        }

        // 心电图
        $mediaArr = $this->media->where(['patient_id'=>$data['patient_id']])
            ->where('url','like','egg%')
            ->get();
        if($mediaArr->isEmpty()){
            $mArr = [];
        }else{
            $mArr = $mediaArr->toArray();
            foreach ($mArr as $kk => $vv) {
                $mArr[$kk]['url'] = 'http://jianshang.care7x24.com/upload/'.$vv['url'];
            }
        }

        $result = [
            'userArr' => $userArr,
            'oneArr' => $oneArr,
            'twoArr' => $twoArr,
            'threeArr' => $tArr,
            'mediaArr' => $mArr,
        ];
        return $result;
    }

    public function getOneInjury($data)
    {
        $this->checkPatientOK($data['patient_id']);
        $res = $this->one->where(['patient_id'=>$data['patient_id']])->get();
        if($res->isEmpty()){
            $this->transFormerE(1002011, '查询数据为空');
        }
        $res = $res[0]->toArray();
        foreach ($res as $kk => $vv){
            if(empty($vv) || $vv == null){
                $res[$kk] = "";
            }
        }
        return $res;
    }

    public function setLevelMess($data)
    {
        $this->checkPatientOK($data['patient_id']);
        $res = $this->two->where(['patient_id'=>$data['patient_id']])->get();
        if($res->isEmpty()){
            $this->transFormerE(1002016, '查询数据为空');
        }
        $this->two->where(['patient_id'=>$data['patient_id']])->update(['level'=>$data['level']]);
        return ['patient_id'=>$data['patient_id']];
    }


}